/**
 * Created by Administrator on 2016/3/30.
 */
var gulp = require("gulp"),
    less = require('gulp-less'),
    browserSync = require('browser-sync'),
    /*imagesmin = require('gulp-imagemin'),*/
    // cache = require('gulp-cache'), // 缓存（图片压缩后不压缩）
    del = require('del'),
    autoprefixer = require('gulp-autoprefixer'),
    plumber = require("gulp-plumber"),
    runSequence = require('run-sequence'),
    htmlmin = require('gulp-htmlmin'), //html压缩
    imagemin = require('gulp-imagemin'), //图片压缩
    pngcrush = require('imagemin-pngcrush'),
    minifycss = require('gulp-minify-css'), //css压缩
    uglify = require('gulp-uglify'), //js压缩
    concat = require('gulp-concat'), //文件合并
    rename = require('gulp-rename'), //文件更名
    notify = require('gulp-notify'), //提示信息
    sourcemaps = require('gulp-sourcemaps'); //sourcemaps

var config = {
    root: './',
    src_less: './less/',
    src_data: './data/',
    src_css: './css/',
    src_images: './images/',
    src_js: './scripts/',
    src_fonts: './fonts/',
    src_html: './'
};

//start browserSync server
gulp.task('browserSync', function() {
    browserSync({
        server: {
            baseDir: [config.root]
        }
    })
});


//编译less文件
gulp.task('less', function() {
    browserSync.notify("正在编译less");
    return gulp.src(config.src_less + "menu.less")
        .pipe(sourcemaps.init())
        .pipe(plumber())
        .pipe(less())
        .pipe(autoprefixer('last 10 versions', 'ie 8'))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(config.src_css))
        .pipe(notify("less编译完成"))
        .pipe(browserSync.reload({
            stream: true
        }))
});

//清除生产文件
// gulp.task('clean',function(){
//     return del.sync('dist').then(function(cb){
//     return cache.clearAll(cb);
//     })
// });

gulp.task('watch', function() {
    gulp.watch(config.src_less + "**/*.less", ['less']); //监听less变化
    gulp.watch(config.src_html + "*.html", browserSync.reload); //监听src/static目录下的html变化
});

gulp.task('default', function(callback) {
    runSequence(['less', 'browserSync', 'watch'], callback); //默认的开发任务，包括编译css，自动刷新浏览器和监听任务
});